package com.kongge.viewdisplayframeworklib.control;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.view.KeyEvent;
import android.view.ViewGroup;

import com.kongge.viewdisplayframeworklib.R;
import com.kongge.viewdisplayframeworklib.event.action.EQAction;
import com.kongge.viewdisplayframeworklib.landscape.AbstractUIManager;
import com.kongge.viewdisplayframeworklib.model.AbsUIController;
import com.kongge.viewdisplayframeworklib.model.NavigateItem;
import com.kongge.viewdisplayframeworklib.model.PageNode;
import com.kongge.viewdisplayframeworklib.model.TabberBarItem;
import com.kongge.viewdisplayframeworklib.sdk.OnSDKExitListener;
import com.kongge.viewdisplayframeworklib.view.NavigateBar;
import com.kongge.viewdisplayframeworklib.view.TabWidget;

public class PublicInterface {

	private static OnSDKExitListener sdkExitListener;
	private static String configFilePath;

	public static void initGlabaManager(String configPath) {
		GlobalMagager.init(configPath);
	}

	public static void setGlobalActivity(Activity activity) {
		GlobalMagager.SetGlabalActivity(activity);
	}

	public static void setParentViewGroup(ViewGroup viewGroup) {
		GlobalMagager.getTabControl().setParentViewGroup(viewGroup);
	}
	
	public static void setTabWidget(TabWidget widget) {
		GlobalMagager.getTabControl().setTabWidget(widget);
	}

	public static void setNavigateBar(NavigateBar navigateBar) {
		GlobalMagager.getNavigateBarControl().SetNavigateBar(navigateBar);
	}

	public static Activity getGloablActivity() {
		return GlobalMagager.getGlobalActivity();
	}

	public static NavigateItem getNavigateItem() {
		return null;
	}

	public static PageNode getPageNodeById(int nPageId) {
		return GlobalMagager.getPageNodeManager().GetPageNodeById(nPageId);
	}

	public static int findLayoutResId(PageNode pageNode) {
		return GlobalMagager.getLayoutIdFactory().FindLayoutResId(pageNode);
	}
	
	public static void refreshBottomBar() {
		AbsUIController control = PublicInterface.getCurUIControl();
		AbstractUIManager tabControl = GlobalMagager.getTabControl();
		if (tabControl != null) {
			if (control.isBottomBarVisible()) {
				tabControl.setTabWidgetHeight(getGloablActivity().getResources().getDimensionPixelSize(R.dimen.bottom_bar_height));
			} else {
				tabControl.setTabWidgetHeight(0);
			}
		}
	}

	public static void refreshNavigateBar() {
		AbsUIController control = PublicInterface.getCurUIControl();
		NavigateBarControl navigateBarControl = GlobalMagager.getNavigateBarControl();
		if (navigateBarControl != null) {
			NavigateItem navigateItem = control.getNavigateItem();
			if (navigateItem == null || navigateItem.isVisible()) {
				GlobalMagager.getNavigateBarControl().refreshNavigateBar(navigateItem);
				navigateBarControl.setHeight(getGloablActivity().getResources().getDimensionPixelSize(R.dimen.navigate_bar_height));
			} else {
				navigateBarControl.setHeight(0);
			}
		}
	}

	public static void showAlert(String string) {
		Dialog alert = new AlertDialog.Builder(
				GlobalMagager.getGlobalActivity()).setTitle("Title")
				.setMessage(string).create();
		alert.show();
	}

	public static void gotoTabbarSection(int nIndex) {
		GlobalMagager.getTabControl().gotoTabSection(nIndex);
	}

	public static void gotoPageWithPageId(int nPageId) {
		GlobalMagager.getTabControl().gotoPage(nPageId);
	}
	
	public static void gotoPageWithPageId(EQAction gotoFrameAction) {
		GlobalMagager.getTabControl().gotoPage(gotoFrameAction);
	}

	public static void goBack() {
		GlobalMagager.getTabControl().goBack();
	}
	
	public static AbsUIController getCurUIControl() {
		return GlobalMagager.getTabControl().getCurUIControlStack().getTopUIControl();
	}

	public static AbsUIController createUIControl(int nPageId) {
		return GlobalMagager.getPageControlCreater().createUIControl(nPageId);
	}
	
	public static AbsUIController createUIControl(EQAction gotoFrameAction) {
		return GlobalMagager.getPageControlCreater().createUIControl(gotoFrameAction);
	}

	public static String getCurPageTitle() {
		return GlobalMagager.getTabControl().getCurUIControlStack()
				.getCurPageTitle();
	}

	public static boolean isCanGoBack() {
		return GlobalMagager.getTabControl().getCurUIControlStack()
				.canGoBack();
	}

	public static int getTabbarItemCount() {
		return GlobalMagager.getPageNodeManager().GetTabberBarItemCount();
	}

	public static TabberBarItem getTabberBarItemByIndex(int nIndex) {
		return GlobalMagager.getPageNodeManager().getTabberBarItemByIndex(
				nIndex);
	}
	
	public static boolean onKeyDown(int keyCode, KeyEvent event) {
		return GlobalMagager.getTabControl().onKeyDown(keyCode,event);
	}
	
	public static void onDestory() {
		GlobalMagager.getTabControl().onDestory();
	}

	public static void exit() {
		if (sdkExitListener != null) {
			sdkExitListener.onExit();
		}
	}

	public static void setSdkExitListener(OnSDKExitListener sdkExitListener) {
		PublicInterface.sdkExitListener = sdkExitListener;
	}

	public static void setConfigFilePath(String configFilePath) {
		PublicInterface.configFilePath = configFilePath;
	}

	public static String getConfigFilePath() {
		return configFilePath;
	}
}
